/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package gui.swing.jxTable;

import gui.swing.jxTable.DownloadableTreeTableModel.DownloadableSegment;
import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;
import javax.swing.table.AbstractTableModel;

/**
 * @author aim
 * @author vinicius
 */
public class DownloadableTableModel extends AbstractTableModel {
    public static final int CHECK_COLUMN            = 0;
    public static final int ITEM_COLUMN             = 1;
    public static final int SIZE_COLUMN             = 2;
    public static final int COLUMN_COUNT            = 3;

    private static final String[] columnIds = {"check", "geral.file", "geral.size"};

    @Override
    public String getColumnName(int column) {
        return columnIds[column];
    }

    
    private final List<DownloadableSegment> downloadables = new ArrayList<DownloadableSegment>();

    public void add(List<DownloadableSegment> newdownloadables) {
        int first = downloadables.size();
        int last = first + newdownloadables.size() - 1;
        downloadables.addAll(newdownloadables);
        fireTableRowsInserted(first, last);
    }

    public List<DownloadableSegment> getDownloadables() {
        return new LinkedList<DownloadableSegment>(downloadables);
    }
    
    public void add(DownloadableSegment downloadable) {
        int index = downloadables.size();        
        downloadables.add(downloadable);
        fireTableRowsInserted(index, index);
    }

    public DownloadableSegment remove(DownloadableSegment downloadable){
        int index = getRowIndex(downloadable);
        return remove(index);
    }
    
    public DownloadableSegment remove(int index){
        try{
            return downloadables.remove(index);
        }finally{
            fireTableRowsDeleted(index, index);
        }        
    }
    
    public int getRowIndex(DownloadableSegment downloadable){
        return downloadables.indexOf(downloadable);
    }
    
    @Override
    public boolean isCellEditable(int rowIndex, int columnIndex) {
        return columnIndex == CHECK_COLUMN;
    }    
    
    @Override
    public int getRowCount() {
        return downloadables.size();
    }

    @Override
    public int getColumnCount() {
        return COLUMN_COUNT;
    }
    
    @Override
    public Class<?> getColumnClass(int column) {
        return getValueAt(0, column).getClass();
    }

    public DownloadableSegment getDownloadable(int row) {
        return downloadables.get(row);
    }

    @Override
    public void setValueAt(Object aValue, int row, int column) {
        if(isCellEditable(row,column)){
            switch (column) {
                case CHECK_COLUMN:
                    getDownloadable(row).checked = (Boolean)aValue;
                    fireTableCellUpdated(row, column);
                    break;
                case ITEM_COLUMN:
                case SIZE_COLUMN:
            }
        }
    }    
    
    @Override
    public Object getValueAt(int row, int column) {
        // PENDING JW: solve in getColumnClass instead of hacking here
        if (row >= getRowCount()) {
            return new Object();
        }
        return getValueAt(getDownloadable(row),column);
    }

    public Object getValueAt(DownloadableSegment d,int column){
        switch (column) {
            case CHECK_COLUMN:
                return d.checked;
            case ITEM_COLUMN:
                return d.item;
            case SIZE_COLUMN:
                return d.size;
        }
        return null;
    }
    
 
    
    
}
